package com.xtck.ha.common.core.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author:zyw
 * @Date:2025/4/24
 */
@Component
@ConfigurationProperties(prefix = "ds.server.datasource")
@Data
public class DsServerConfig {
    private String url;
    private String masterDb;
    private String slaveDb;

    @PostConstruct
    public void init() {
        URL = this.url;
        MASTER_DB = this.masterDb;
        SLAVE_DB = this.slaveDb;
    }

    public static String  URL;
    public static String  MASTER_DB;
    public static String  SLAVE_DB;

    public String getUrlOnly(String name){
        if (name==null||name.trim().isEmpty()){
            return URL +"/";
        }else {
            return URL +"/"+name+"/";
        }

    }

    public String getUrlFree(String masterName,String tableName){
        return URL +"/"+masterName+"/"+tableName;

    }

    public String getUrl(){
        return URL +"/"+MASTER_DB+"/";
    }

    public String getUseDb(String dbflag){
        switch (dbflag){
            case "slave":
                return SLAVE_DB;
            default:
                return MASTER_DB;
        }
    }

    public String getUrlWithTableAndFlag(String tableName,String dbflag){
        return URL +"/"+getUseDb(dbflag)+"/"+tableName;
    }

}
